# -*- coding: utf-8 -*-
import random
import re

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

from fw.verifier import *
from fw import navigate
from fw import movie


class TestsAddMovie:

    def test_add_movie(self, browser):
        navigate.goto_main_page(False)
        movie_list_old = movie.get_movie_list(browser)

        browser.find_element_by_xpath(".//a/img[@title='Add movie']").click()
        assert(is_element_present(browser, By.XPATH, ".//h2[text()='Movie information']"))

        movie_name = 'movie_' + str(random.randrange(10000, 99999))
        movie_year = str(random.randrange(1900, 2015))

        browser.find_element_by_name('name').send_keys(movie_name)
        browser.find_element_by_name('year').send_keys(movie_year)
        browser.find_element_by_xpath(".//a/img[@title='Save']").click()

        movie_title = browser.find_element_by_xpath(".//div[@class='maininfo_full']/h2").text
        assert(movie_title == movie_name + " (" + movie_year + ")")

        navigate.goto_main_page(False)
        verify_list_incremented(movie_list_old, movie.get_movie_list(browser), movie_name)


    def test_required_field_error(self, browser):
        navigate.goto_main_page(False)
        movie_list_old = movie.get_movie_list(browser)

        browser.find_element_by_xpath(".//a/img[@title='Add movie']").click()
        assert(is_element_present(browser, By.XPATH, ".//h2[text()='Movie information']"))

        movie_name = 'movie_name'

        browser.find_element_by_name('name').send_keys(movie_name)
        browser.find_element_by_xpath(".//a/img[@title='Save']").click()

        err_label = browser.find_element_by_xpath(".//label[@for='year']")
        assert(err_label.is_displayed())
        assert(err_label.text == 'This field is required')

        navigate.goto_main_page(False)
        verify_list_not_changed(movie_list_old, movie.get_movie_list(browser))


class TestsRemoveMovie:

    def test_remove_movie(self, browser):
        navigate.goto_main_page(False)
        movie_name = movie.get_movie_name(browser)

        movie_list_old = movie.get_movie_list(browser)

        browser.find_element_by_xpath(".//div[@title='" + movie_name + "']").click()
        movie_title = browser.find_element_by_xpath(".//div[@class='maininfo_full']/h2").text
        assert(movie_name in movie_title)

        remove_button = browser.find_element_by_xpath(".//a/img[@title='Remove']/..")
        alert_text = re.search("\('(.+)'\)", remove_button.get_attribute('onclick')).group(1)

        remove_button.click()

        alert = browser.switch_to.alert
        assert(alert.text == alert_text)
        alert.accept()

        assert(is_element_present(browser, By.XPATH, ".//div[@class='searchbox']/input[@value='Search for movies...']"))
        verify_list_decremented(movie_list_old, movie.get_movie_list(browser), movie_name)


class TestsFindMovie:

    def test_find_movie(self, browser):
        navigate.goto_main_page(False)
        movie_name = movie.get_movie_name(browser)

        search_input = browser.find_element_by_id('q')
        search_input.clear()
        search_input.send_keys(movie_name, Keys.ENTER)

        movie_list = movie.get_movie_list(browser)

        assert(len(movie_list) > 0)
        assert(movie_list.pop() == movie_name)


    def test_nothing_finded(self, browser):
        navigate.goto_main_page(False)

        search_input = browser.find_element_by_id('q')
        search_input.clear()
        search_input.send_keys('expected empty search result', Keys.ENTER)

        movie_list = movie.get_movie_list(browser)

        assert(len(movie_list) == 0)
        assert(browser.find_element_by_xpath(".//div[@id='results']/div[@class='content']").text == 'No movies where found.')